import { effectScope, onScopeDispose, watch } from 'vue'
import { useFullscreen } from '@vueuse/core'
import { JBoltApi } from '@/utils'
import { useAppStore } from '../modules'

async function loadAppData(app: any) {
    JBoltApi.get('/api/globalConfig/copyrightAndApprove').then((res) => {
        const data = res.result.data
        app.setCopyright(data.copyright, data.copyrightLink)
        app.setWebsiteApprove(data.approveSn, data.approveLink)
    })
}
/** 订阅app store */
export default function subscribeAppStore() {
    const { isFullscreen, toggle } = useFullscreen()

    const app = useAppStore()
    loadAppData(app)

    const scope = effectScope()

    function update() {
        if (app.contentFull && !isFullscreen.value) {
            toggle()
        }
        if (!app.contentFull && isFullscreen.value) {
            toggle()
        }
    }

    scope.run(() => {
        watch(
            () => app.contentFull,
            () => {
                update()
            }
        )

        watch(isFullscreen, (newValue) => {
            if (!newValue) {
                app.setContentFull(false)
            }
        })
    })

    onScopeDispose(() => {
        scope.stop()
    })
}
